package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.*;
import sanrenxing.dayEnd.backRepertoryTotal.service.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:权益处理控制器类
 * 编写者：张港峰
 * 日期：2021
 * 版本：1.0
 */
@RestController
@RequestMapping("/equityCrud")
public class EquityController {
    //定义权益处理业务层接口 Equ
    @Autowired
    private EquityBiz equityBiz;
    //定义证券应收应付库存业务层接口 SecRec
    @Autowired
    private SecRecAndPayStockBiz srapBiz;
    //定义现金应收应付库存业务层接口 CashRec
    @Autowired
    private CashRecAndPayStockBiz crapBiz;
    //定义现金应收应付业务层接口 Money
    @Autowired
    private MoneyitiesService moneyitiesService;
    //定义资金调拨的业务层接口 Fund
    @Autowired
    private FundAllocationBiz fundAllocationBiz;

    @GetMapping("/equity")
    public Map<String, Object> addequity(HttpServletRequest req, int page, int limit){
        /*定义用于多条件查询的权益处理对象*/
        Equity equity=new Equity();
        /*接收来自请求中的权益类型 rigType*/
        String rigType=req.getParameter("rigType");
        if(rigType!=null && rigType.trim().length()!=0){
            equity.setRigType(Integer.parseInt(rigType));
        }
        /*接收来自请求中的状态 proState*/
        String proState=req.getParameter("proState");
        if(proState!=null && proState.trim().length()!=0){
            equity.setProState(Integer.parseInt(proState));
        }

        /*调用业务层获取查询条件方法*/
        String condition=equityBiz.getCount(equity);
        /*执行多条件分页查询的方法*/
        List<EquityTwo> equityList=equityBiz.findByPages(limit,page,condition);
        /*条件记录总行数*/
        int totalRows=equityBiz.count(condition);

        /*定义映射集合对象*/
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",equityList);
        map.put("count",totalRows);
        //返回集合对象
        return map;
    }

    /*删除一行数据*/
    @DeleteMapping("/equity/{rightsId}")
    public String delete(@PathVariable int rightsId){
        return equityBiz.delete(rightsId)>0?"delete success":"delete failure";
    }

    /*
     *权益处理点击处理时
     */
    @PostMapping("/allocation/{rightsId}")
    public String account(@PathVariable String rightsId){
        if (rightsId.contains(",")) {
            //对交易数据编号字符串进行分割
            String[] strs = rightsId.split(",");
            //循环查询
            for (String str : strs) {
                //调用业务层查询权益处理数据
                Equity equity=equityBiz.findById(Integer.parseInt(str));
                //定义证券应收应付库存对象 SecRec
                SecRecAndPayStock srap=new SecRecAndPayStock();
                //定义现金应收应付对象 Cash
                CashReceivable cashReceivable=new CashReceivable();
                //定义现金应收应付库存对象 CashRec
                CashRecAndPayStock crap=new CashRecAndPayStock();
                //定义资金调拨对象 Fund
                FundAllocation fundAllocation=new FundAllocation();

                //判断权益处理状态，如果权益处理状态为未结算 Pro
                if(equity.getProState()==-1){
                    //判断权益类型，如果权益类型为分红 Rig
                    if(equity.getRigType()==1){
                        //设置现金应收应付的现金账户编号 CaId
                        cashReceivable.setCaId(equity.getCaId());
                        //设置现金应收应付的业务日期 BusinDate
                        cashReceivable.setBusinDate(equity.getRighDay());
                        //设置现金应收应付的金额 SettMoney
                        cashReceivable.setMoney(equity.getSettMoney());
                        //设置现金应收应付的基金编号 FundId
                        cashReceivable.setFundId(equity.getFundId());
                        //设置现金应收应付的统计状态 States
                        cashReceivable.setStates(0);
                        //设置现金应收应付的业务类型为分红 BusinType
                        cashReceivable.setBusinType("分红");
                        //设置现金应收应付的资金流向为流入 CapFlow
                        cashReceivable.setCapFlow(1);
                        //调用现金应收应付的业务层添加方法
                        moneyitiesService.add(cashReceivable);

                        //设置资金调拨的基金编号 fundId
                        fundAllocation.setFundId(equity.getFundId());
                        //设置资金调拨的现金账户编号 caId
                        fundAllocation.setCaId(equity.getCaId());
                        //设置资金调拨的调拨金额 reMoney
                        fundAllocation.setReMoney(equity.getSettMoney());
                        //设置资金调拨的调拨日期 reDate
                        fundAllocation.setReDate(equity.getRighDay());
                        //设置资金调拨的业务日期 businDate
                        fundAllocation.setBusinDate(equity.getRighDay());
                        //设置资金调拨的调拨类型为分红 reType
                        fundAllocation.setReType("分红");
                        //设置资金调拨的调拨方向为流入 dial
                        fundAllocation.setDial(1);
                        //设置资金调拨的标记为未处理 mark
                        fundAllocation.setMark("未处理");
                        //调用资金调拨的业务层添加方法
                        fundAllocationBiz.add(fundAllocation);

                    }else{//权益类型为送股
                        //设置证券应收应付库存的证券编号 seId
                        srap.setSeId(equity.getSeId());
                        //设置证券应收应付库存的基金编号 fundId
                        srap.setFundId(equity.getFundId());
                        //设置证券应收应付库存的总金额 sunMoney
                        srap.setSumMoney(0);
                        //设置证券应收应付库存的证券数量 secNum
                        srap.setSecNum(equity.getBonusNum());
                        //设置证券应收应付库存的统计日期 dates
                        srap.setDates(new SimpleDateFormat("yyyy-MM-dd").format(equity.getRighDay()));
                        //设置证券应收应付库存的现金账户编号 caId
                        srap.setCaId(equity.getCaId());
                        //设置证券应收应付库存的业务类型为送股 businType
                        srap.setBusinType("送股");
                        //设置证券应收应付库存的业务状态为流出 businStat
                        srap.setBusinStat(-1);
                        //调用证券应收应付库存业务层的添加方法
                        srapBiz.add(srap);
                    }
                    //将权益处理状态改为已结算 prostate
                    equity.setProState(1);
                    //调用权益处理业务层的修改方法
                    equityBiz.update(equity);
                }
            }
        }
        return "account success";
    }
}
